/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alessio
 */
@WebServlet(name = "ControllerServlet", 
            loadOnStartup = 1,               //inizializza e istanzia al momento del deploy         
            urlPatterns = {"/registrazione",
                            "/login",
                            "/logout",
                            "/profilo",
                            "/miei_annunci"})
public class ControllerServlet extends HttpServlet {

 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();

        // if category page is requested
        if (userPath.equals("/registrazione")) {
            // TODO: Implement registrazione request
            
            userPath = "/modulo_registrazione";

        // if cart page is requested
        } else if (userPath.equals("/login")) {
            // TODO: Implement cart page request

            userPath = "/index";

        // if checkout page is requested
        } else if (userPath.equals("/logout")) {
            // TODO: Implement checkout page request
            userPath = "/index";
        // if user switches language
            
            
        } else if (userPath.equals("/profilo")) {
            // TODO: Implement language request
            
            userPath = "/view_profilo";

        }

         else if (userPath.equals("/miei_annunci")) {
            // TODO: Implement language request
            
            userPath = "/miei_annunci";

        }
        
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String userPath = request.getServletPath();

        // if addToCart action is called
        if (userPath.equals("/addToCart")) {
            // TODO: Implement add product to cart action

        // if updateCart action is called
        } else if (userPath.equals("/updateCart")) {
            // TODO: Implement update cart action

        // if purchase action is called
        } else if (userPath.equals("/purchase")) {
            // TODO: Implement purchase action

            userPath = "/confirmation";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}